package admin

import (
	"github.com/gin-gonic/gin"
	"go-project/dto/requrest"
	"go-project/pkg/utils"
	"go-project/service"
	"net/http"
	"strconv"
)

type PermissionsController struct {
}

func (PermissionsController) List(c *gin.Context) {
	lists, count, err := service.Permissions{}.FindList(c.Query("name"))
	if err != nil {
		utils.Error(c, utils.ERROR, "权限数据数据加载失败", http.StatusBadRequest)
	}
	utils.PaginationSuccess(c, count, lists, requrest.Pagination{})
}
func (PermissionsController) Add(c *gin.Context) {
	permissionsInsertRequest := &requrest.PermissionsInsertRequest{}
	errors := permissionsInsertRequest.BindAndValidate(c)
	if errors != nil {
		return
	}
	insert_id, errors := service.Permissions{}.Add(permissionsInsertRequest)
	if errors != nil {
		utils.Error(c, utils.ERROR, "添加失败", http.StatusOK)
		return
	}
	utils.Success(c, gin.H{
		"id": insert_id,
	}, http.StatusCreated)
}

func (PermissionsController) ListData(c *gin.Context) {
	lists, _, err := service.Permissions{}.FindList(c.Query("name"))
	if err != nil {
		utils.Error(c, utils.ERROR, "权限数据数据加载失败", http.StatusBadRequest)
	}
	permissions := make([]*utils.Permission, 0, 10)
	for _, list := range lists {
		parentId := func(id *uint) uint {
			if id == nil {
				return 0
			} else {
				return *id
			}
		}(list.ParentID)
		permissions = append(permissions, &utils.Permission{
			ID:       list.ID,
			Name:     list.DisplayName,
			ParentID: parentId,
			Children: []*utils.Permission{},
		})
	}
	utils.BuildPermissionTree(permissions)
	for i := 0; i < len(permissions); i++ {
		if permissions[i].ParentID != 0 {
			// 如果当前权限非根节点，直接删除它
			permissions = append(permissions[:i], permissions[i+1:]...)
			i-- // 由于删除了一个元素，需要将索引向前移动
		}
	}
	utils.Success(c, gin.H{
		"lists": permissions,
	}, http.StatusOK)

}

func (PermissionsController) Edit(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		c.HTML(http.StatusNotFound, "admin.error", gin.H{
			"error": err.Error(),
		})
		return
	}
	model, err := service.Permissions{}.GetById(uint(id))
	if err != nil {
		c.HTML(http.StatusBadGateway, "admin.error", gin.H{
			"error": err.Error(),
		})
		return
	}

	c.HTML(http.StatusOK, "permissions.edit", gin.H{
		"model": model,
	})
}

func (PermissionsController) Update(c *gin.Context) {
	permissionsUpdateRequest := &requrest.PermissionsUpdateRequest{}
	err := permissionsUpdateRequest.BindAndValidate(c)
	if err != nil {
		return
	}
	_, err = service.Permissions{}.Update(permissionsUpdateRequest)
	if err != nil {
		utils.Error(c, utils.ERROR, err.Error(), http.StatusOK)
		return
	}
	utils.Success(c, gin.H{}, http.StatusNoContent)
}

func (PermissionsController) Delete(c *gin.Context) {
	value := c.Query("id")
	if value == "" {
		utils.Error(c, utils.ERROR, "id不能为空", http.StatusOK)
		return
	}
	id, err := strconv.Atoi(value)
	if err != nil {
		utils.Error(c, utils.ERROR, err.Error(), http.StatusOK)
		return
	}
	rowsAffected, err := service.Permissions{}.Del(uint(id))
	if err != nil {
		utils.Error(c, utils.ERROR, err.Error(), http.StatusOK)
		return
	}
	if rowsAffected <= 0 {
		utils.Error(c, utils.ERROR, "删除失败", http.StatusOK)
	}
	utils.Success(c, gin.H{}, http.StatusNoContent)
}
